package com.wlf.template;

import com.wlf.constant.Constants;
import com.wlf.constant.DateBuild;
import com.wlf.entity.ProjectAddress;
import com.wlf.util.PrintUtil;
import org.plutext.jaxb.xslfo.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author ex_weilingfeng
 *
 */
@Component
public class PolicyTemplate {



/**
 * 返回一个Root对象
 */
	public Root createRoot(){
		Root root = new Root();
		root.setFontFamily("SimSun"); //设置字体
		LayoutMasterSet layMas = new LayoutMasterSet();

		SimplePageMaster simplePageMaster =new SimplePageMaster();

		simplePageMaster=buildPageLayout(simplePageMaster);
		layMas.getSimplePageMasterOrPageSequenceMaster().add(simplePageMaster);
		root.setLayoutMasterSet(layMas);
		root.getPageSequence().add(createPageSequence(new PageSequence()));

		return root;
	}

	/**
	 * 构造页面布局
	 * @param simplePageMaster
	 * @return
	 */
	public SimplePageMaster buildPageLayout(SimplePageMaster simplePageMaster) {

		simplePageMaster.setMasterName("healthy");
		simplePageMaster.setPageWidth("210mm");//页宽
		simplePageMaster.setPageHeight("300mm");//页高
		simplePageMaster.setMarginTop("4mm");//页上边距
		simplePageMaster.setMarginBottom("4mm");//页下边距
		simplePageMaster.setMarginLeft("8mm");//左边距
		simplePageMaster.setMarginRight("8mm");//右边距

		RegionBody  regionBody = new RegionBody();
		regionBody.setBackgroundRepeat(BackgroundRepeatType.NO_REPEAT); //设置不平铺
		regionBody.setBackgroundImage("http://lingfeng.shop/yaoyao/DRAFT.jpg");
		regionBody.setBackgroundPositionHorizontal("50%");//水平居中
		regionBody.setBackgroundPositionVertical("50%");//竖直居中

		regionBody.setMarginTop("4.5cm");//上边距
		regionBody.setMarginBottom("3cm");//页底部边距
		regionBody.setMarginLeft("15pt");//左边距
		regionBody.setMarginRight("15pt");//右边距


		simplePageMaster.setRegionBody(regionBody);

		RegionBefore rbefore = new RegionBefore();
		rbefore.setExtent("3.6cm"); //页眉
		simplePageMaster.setRegionBefore(rbefore);

		RegionAfter rafter = new RegionAfter();
		rafter.setExtent("2.5cm");//页脚
		simplePageMaster.setRegionAfter(rafter);

		RegionStart rstart = new RegionStart();
		rstart.setExtent("15pt");//左侧栏
		simplePageMaster.setRegionStart(rstart);

		RegionEnd rend = new RegionEnd();
		rend.setExtent("15pt");
		simplePageMaster.setRegionEnd(rend);

		return simplePageMaster;
	}

	/**
	 * 组装page-sequence对象
	 * @throws Exception
	 */
	public PageSequence createPageSequence(PageSequence pageSequence){
		pageSequence.setMasterReference("healthy");
		for(int i =0;i<2 ;i++){
			StaticContent  staticContent = new StaticContent();
			if(i==0){
				pageSequence.getStaticContent().add(pageHeader(staticContent));
			}else if(i==1){
				pageSequence.getStaticContent().add(pageFooter(staticContent));
			}
		}
		Flow flow = new Flow();
		pageSequence.setFlow(makeFirstFlow(flow));

		return pageSequence;
	}

	//页眉
	 public StaticContent pageHeader(StaticContent content){

		 content.setFlowName("xsl-region-before");
		 ExternalGraphic graphic = new ExternalGraphic();
		 graphic.setSrc("http://lingfeng.shop/yaoyao/baidu.jpg");
		 graphic.setContentWidth("4cm");
		 graphic.setContentHeight("2cm");
		 Block blockGraphic = new Block();
		 blockGraphic.getContent().add(graphic);
		 content.getBlockOrBlockContainerOrTable().add(blockGraphic);

		 return content;
	  }

	// 页脚
	public StaticContent pageFooter(StaticContent content){
		Block block;
		content.setFlowName("xsl-region-after");

		block = new Block();
		block.setTextAlign(TextAlignType.END);
		block.setFontSize("10.5pt");
		block.setSpaceBefore("15pt");
		block.setMarginTop("1cm");
		PageNumber pageNum = new PageNumber();
		block.getContent().add(pageNum);
		block.getContent().add("页");
		block.getContent().add("\r\n/");
		PageNumberCitation pagNmCat = new PageNumberCitation();
		pagNmCat.setRefId("TheVaryLastPage");
		block.getContent().add(pagNmCat);
		block.getContent().add("页");
		content.getBlockOrBlockContainerOrTable().add(block);

		return content;
	}

	public  Flow makeFirstFlow(Flow flow){

		flow.setFlowName("xsl-region-body");
		Block block;
		TableRow tableRow;
		TableCell tableCell;

		StringBuffer sb = new StringBuffer();

		sb.append("央视主播提醒称：试图拐卖儿童的人，别动歪脑筋！有拐必打，使命必达！\r\n");
		sb.append("网友称：\r\n");
		sb.append("孩子对于父母而言那就是心头肉，生生被人挖走，活不见人死不见尸。那种痛以及因此带来的后果会殃及很多人好几代，改写很多人的人生。还是判的不够重，才让人贩子铤而走险！\r\n");
		sb.append("如果身份证中有基因信息，孩子一出生就测量基因并输入到派出所，孩子办身份证和户口须再采集基因数据，这买了孩子上不了户口没有身份证，估计没几个人会再买孩子吧？\r\n");
		sb.append("痛恨人贩子是自然的，如果是法律判罚太重，对于被拐卖儿童不利，人贩子会不会狗急跳墙？毕竟父母找没找到孩子，也不希望孩子受到伤害呀！\r\n");
		sb.append("两个人贩子以为自己拐的阿猫阿狗，几年就会忘记？他们忘了父爱如山，也许根本没有想到父亲会用24年死磕！他们也许看过失孤，但是没想到就是自己造成的悲剧！所幸，孩子在一个正常家庭成长！这是不幸中的万幸！愿天下无拐！\r\n");
		sb.append("最关键在于家属的不放弃，否则的话。哎，希望越来越完善，越来越好吧。\r\n");
		sb.append("最可怕的是孩子被拐，后来亲生父母找到了，孩子却不认自己亲生母亲，只认养母，这才是最大的悲痛！\r\n");

		String [] array = sb.toString().split("\r\n");

		for(String str : array){
			flow.getMarkerOrBlockOrBlockContainer().add(PrintUtil.createBlock(str,"SimSun",null,"20pt","20pt"));
		}

		Table table =PrintUtil.createTale("100%","20%","50%","15%","15%");
		TableBody tableBody=PrintUtil.createTablebody("10pt",null,"SimSun");

		List<ProjectAddress> projectAddressList =DateBuild.projectAddressList;

		for (ProjectAddress projectAddress:projectAddressList){
			tableRow = new TableRow();
			for (int j=0;j<4;j++){
				tableCell = PrintUtil.createTableCell("0.05mm",TextAlignType.CENTER,BorderStyleType.DOUBLE,"20pt");
				switch (j){
					case 0 :
						PrintUtil.createTableRow(tableRow,tableCell,projectAddress.getProjectName(),"SimSun","10pt");
						break;
					case 1 :
						PrintUtil.createTableRow(tableRow,tableCell,projectAddress.getUrl(),"SimSun","10pt");
						break;
					case 2:
						PrintUtil.createTableRow(tableRow,tableCell,projectAddress.getUserName(),"SimSun","10pt");
						break;
					case 3 :
						PrintUtil.createTableRow(tableRow,tableCell,projectAddress.getPassWord(),"SimSun","10pt");
						break;
				}
			}
			tableBody.getTableRow().add(tableRow);

		}
		table.getTableBody().add(tableBody);
		flow.getMarkerOrBlockOrBlockContainer().add(table);


		block=PrintUtil.createBlock("甲方： 上海百度有限公司","SongHei","12pt",null,"20pt");
		block.setSpaceBefore("200pt");
		block.setTextAlign(TextAlignType.END);
		flow.getMarkerOrBlockOrBlockContainer().add(block);

		block=PrintUtil.createBlock("乙方（司机）： 李彦宏","SongHei","12pt",null,"20pt");
		block.setTextAlign(TextAlignType.END);
		flow.getMarkerOrBlockOrBlockContainer().add(block);

		//盖章
		flow.getMarkerOrBlockOrBlockContainer().add(PrintUtil.creaExternalGraphic("http://lingfeng.shop/yaoyao/seal-1.png","4cm","4cm","13.5cm","-4cm"));

		block=PrintUtil.createBlock("签署时间 ："+new Date(),"SongHei",null,"-25pt",null);
		block.setTextAlign(TextAlignType.END);
		flow.getMarkerOrBlockOrBlockContainer().add(block);



		//页码
		block=new Block();
		block.setId("TheVaryLastPage");
		flow.getMarkerOrBlockOrBlockContainer().add(block);

		return flow;
	}

}
